package com.sqoop.server.factory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sqoop.core.destnation.MqMsgDestination;
import com.sqoop.server.BaseProcess;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by - on 2017/11/9.
 */
public class TaskManager {

    private static Logger logger = LogManager.getLogger(TaskManager.class.getName());

    private static TaskManager taskManager = new TaskManager();

    public TaskManager(){

    }

    public static TaskManager getInstance(){
        return taskManager;
    }

    Map<String, ProcessConfig> processConfigMap = new HashMap<>();
    Map<String, BaseProcess> baseProcessMap = new HashMap<>();

    public void load(String str) {
        File file = new File(str);
        if (!file.exists()){
            return;
        }
        Resource resource = new FileSystemResource(file);
        String configStr = null;
        try {
            configStr = IOUtils.toString(resource.getInputStream());
            List<ProcessConfig> processConfigs = new Gson().fromJson(configStr, new TypeToken<List<ProcessConfig>>() {}.getType());
            for (ProcessConfig processConfig:processConfigs){
                processConfigMap.put(processConfig.getServicename(),processConfig);
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("e",configStr);
        }
    }




    public void start() {
        for (Map.Entry<String, ProcessConfig> entry : processConfigMap.entrySet()){
            ProcessConfig processConfig = entry.getValue();
            String classname = processConfig.getClassname();
            try {
                Class clazz = Class.forName(classname);
                BaseProcess baseProcess  = (BaseProcess)clazz.newInstance();
                baseProcess.setTaskId(processConfig.getServicename());
                baseProcess.start();
                baseProcessMap.put(entry.getKey(), baseProcess);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

    }

    public void stop() {
        for (Map.Entry<String, BaseProcess> entry:baseProcessMap.entrySet()){
            entry.getValue().stop();
        }
    }

    public void stopById(String taskid) {
        if (baseProcessMap.containsKey(taskid) && baseProcessMap.get(taskid) != null){
            baseProcessMap.get(taskid).stop();
        }
    }

    public void pause() {
        for (Map.Entry<String, BaseProcess> entry:baseProcessMap.entrySet()){
            entry.getValue().pause();
        }

    }

    public void pauseById(String taskid) {
        if (baseProcessMap.containsKey(taskid) && baseProcessMap.get(taskid) != null){
            baseProcessMap.get(taskid).pause();
        }

    }


    public void resume() {
//        baseProcess.resume();
    }

    public void resumeById(String msgId) {
//        baseProcess.resumeById(msgId);
    }

    public Map<String, ProcessConfig> getProcessConfigMap() {
        return processConfigMap;
    }

    public void setProcessConfigMap(Map<String, ProcessConfig> processConfigMap) {
        this.processConfigMap = processConfigMap;
    }

    public Map<String, BaseProcess> getBaseProcessMap() {
        return baseProcessMap;
    }

    public void setBaseProcessMap(Map<String, BaseProcess> baseProcessMap) {
        this.baseProcessMap = baseProcessMap;
    }
}